// pages/category/index.js
// import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog.js'
// import Dialog from '@vant/weapp/dialog/dialog';
Page({
  data: {
    cheferId:'',
    cheferName:'',
    cheferAvatarUrl:'',
    formattedTime:'',
    isShow:false,
    _id:'',
    description:'',
    showDialog1:false,
    showDialog:false,
    isLoggedIn:false,
    userInfo:{},
    categories: [], // 分类数据
    loading: true, // 加载状态
    db:null,
  },
  onLoad(options) {
    // 在任何页面/组件中获取
    const app = getApp();
    const userInfo = wx.getStorageSync('userInfo');
    console.log('business userInfo',userInfo)
    this.setData({
      isLoggedIn: !!userInfo,
      userInfo: userInfo || {}
    });
    console.log(app.globalData.env); // 输出: your-env-id
    // 获取默认环境的数据库引用
    this.setData({
      db:app.globalData.db
    })
    console.log('onLoad options',options)
    if(app.globalData.detail){
      const allDetail = JSON.parse(decodeURIComponent(app.globalData.detail))
      console.log('allDetail',allDetail)
      this.setData({
        categories:allDetail.goodsList,
        cheferId:allDetail.cheferId,
        cheferName:allDetail.cheferName,
        cheferAvatarUrl:allDetail.cheferAvatarUrl,
      })
    }
    if(options.id){
      this.data.db.collection('goodsList').where({
        _id:options.id
      }).get()
      .then(res => {
        console.log('optiosn id res',res)
        if(res.data && res.data.length > 0){
          this.setData({
            isShow:true,
            categories:res.data[0].goodsList,
            description:res.data[0].description,
            formattedTime:this.getFormats(res.data[0].createTime),
          })
        }else{

        }
      }).catch(err => {

      }).finally(() => {

      })
    }
    if(options && options.activeId){
      app.globalData.activeId = options.activeId
    }
  },
  toOpen(){
    this.setData({
      showDialog:true
    })
  },
  // 提交表单
  submitForm() {
    const formData = {
      goodsList:this.data.categories,
      description: this.data.description,
      customerId:this.data.userInfo._openid,
      customerName:this.data.userInfo.nickName,
      customerAvatarUrl:this.data.userInfo.avatarUrl,
      cheferId:this.data.cheferId,
      cheferName:this.data.cheferName,
      cheferAvatarUrl:this.data.cheferAvatarUrl,
      status:'未完成'
    }
    wx.showLoading({
      title: '下单中...',
    })
    formData.createTime = new Date()
    this.data.db.collection('goodsList').add({
      data: formData,
      success: res => {
        this.data._id = res._id
        const app = getApp()
        delete app.globalData.detail
        wx.hideLoading()
        wx.showToast({
          title: '下单成功',
          icon: 'success'
        })
        this.setData({
          showDialog1:true
        })
      },
      fail: err => {
        console.error('下单失败', err)
        wx.hideLoading()
        wx.showToast({
          title: '下单失败',
          icon: 'none'
        })
      }
    })
  },
  keepAdd(){
    this.setData({
      showDialog:false
    })
  },
  back(){
    this.setData({
      showDialog1:false
    })
    wx.reLaunch({
      url: '/pages/business/index',
    })
  },
  toDetail(){
    this.setData({
      showDialog:false,
      showDialog1:false
    })
    wx.navigateTo({
      url: `/pages/orderDetail/index?id=${this.data._id}&currentTab=${this.data.currentTab}&back=business`,
    })
  },
    // 描述变化
    onDescriptionChange(e) {
      this.setData({
        description: e.detail.value
      })
    },
    getFormats(timestamp) {
      const date = new Date(timestamp);
    
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
    
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
});